﻿using System;
using System.Data;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Framework.Providers;
using RegistryManager.Components.Data.Providers;

namespace RegistryManager.Components.Data.Model
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// An abstract class for the data access layer
    /// 
    /// The abstract data provider provides the methods that a control data provider (sqldataprovider)
    /// must implement. You'll find two commented out examples in the Abstract methods region below.
    /// </summary>
    /// -----------------------------------------------------------------------------
    public abstract class DataProvider
    {

        #region Shared/Static Methods

        private static DataProvider provider;

        // return the provider
        public static DataProvider Instance()
        {
            return provider ?? (provider = new SqlDataProvider());
            //if (provider == null)
            //{
            //    const string assembly = "RegistryManager.Components.Data.Providers.SqlDataprovider,RegistryManager";
            //    Type objectType = Type.GetType(assembly, true, true);

            //    provider = (DataProvider)Activator.CreateInstance(objectType);
            //    DataCache.SetCache(objectType.FullName, provider);
            //}

            //return provider;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Not returning class state information")]
        public static IDbConnection GetConnection()
        {
            const string providerType = "data";
            ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration(providerType);

            Provider objProvider = ((Provider)_providerConfiguration.Providers[_providerConfiguration.DefaultProvider]);
            string _connectionString;
            if (!String.IsNullOrEmpty(objProvider.Attributes["connectionStringName"]) && !String.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings[objProvider.Attributes["connectionStringName"]]))
            {
                _connectionString = System.Configuration.ConfigurationManager.AppSettings[objProvider.Attributes["connectionStringName"]];
            }
            else
            {
                _connectionString = objProvider.Attributes["connectionString"];
            }

            IDbConnection newConnection = new System.Data.SqlClient.SqlConnection();
            newConnection.ConnectionString = _connectionString.ToString();
            newConnection.Open();
            return newConnection;
        }

        #endregion

        #region Abstract methods

        //public abstract IDataReader GetItems(int userId, int portalId);

        //public abstract IDataReader GetItem(int itemId);        

        public abstract IDataReader GetWishLists(/*int moduleID*/);
        //public abstract void GetWishLists(/*int moduleID*/);

        //public abstract IDataReader GetWishList(int wishListId);

        //public abstract void DeleteTask(int taskId);

        //public abstract void DeleteTasks(int moduleId);

        //public abstract int AddTask(Task t);

        //public abstract void UpdateTask(Task t);

        #endregion

    }

}